<?php
/**
 * 短信发送记录
 * Created by PhpStorm.
 * User: liyy
 * Date: 2017/7/27 0027
 * Time: 下午 3:39
 */

namespace app\api\model;

use think\Db;
use think\Model;
use \think\Log;
class DxSendCode extends Model{

    /**
     * 搜索该手机号下的短信验证码
     */
    public function findDxList($data){
        if (empty($data['phone'])){
            return false;
        }else{
            $where['mobile'] = array('eq',$data['phone']);
        }
        if ($data['code']){
            $where['code'] = array('eq',$data['code']);
        }
        $where['sendtime'] = array('lt',time());
        $where['killtime'] = array('gt',time());
        $where['check'] = array('eq',0);
        $where['source'] = array('eq',$data['type']);
        $result = Db::name('dxsend_log')->field('sendtime,code')->where($where)->find();
        if (empty($result)){
            return false;
        }else{
            return $result;
        }
    }

    /**
     * 发送短信
     */
    public function sendDx($data){
        if (empty($data['phone'])){
            return false;
        }
        if ($data['type'] !=1 && $data['type'] != 2 && $data['type'] != 3 && $data['type'] != 4){
            return false;
        }
        //kill时间
        $map['mobile'] = $data['phone'];
        $map['source'] = $data['type'];
        $map['check'] = 0;
        $map['code'] = $data['code'];
        $map['createtime'] = time();
        $map['sendtime'] = time();
        $map['killtime'] = time()+30*60;
        $result = Db::name('dxsend_log')->insert($map);
        return $result;
    }

    /**
     * 更新短信发送记录
     */
    public function updateDx($data){
        if (empty($data['code']) || empty($data['phone']) || empty($data['sendtime'])){
            return false;
        }
        if ($data['type'] !=1 && $data['type'] != 2 && $data['type'] != 3 && $data['type'] != 4){
            return false;
        }
        $where['source'] = $data['type'];
        $where['sendtime'] = $data['sendtime'];
        $where['mobile'] = $data['phone'];
        $where['code'] = $data['code'];
        $where['check'] = 0;
        if ($data['check'] == 1){
            $map['check'] = 1;
        }
        $map['sendtime'] = time();
        $result = Db::name('dxsend_log')->where($where)->update($map);
        return $result;

    }
}